<?php

//判断网址
function is_url($str){
  // return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"])*$/", $str);
  return preg_match("/(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?/", $str);
}
function toDatetime( $time, $format = 'Y-m-d H:i:s' ) {
  if ( empty ( $time ) ) {
    $time =  time();
  }
  if ( is_numeric( $time ) ) {
    return date( $format, $time );
  }
  $format = str_replace( '#', ':', $format );
  return date( $format, strtotime( $time ) );
}

function toDate( $time, $format = 'Y-m-d' ) {
  if ( empty ( $time ) ) {
    $time =  time();
  }
  $format = str_replace( '#', ':', $format );
  return date( $format, $time );
}
function filterueditor( $value ) {
  $str = str_replace( "\n", "", $value );
  $str = str_replace( "\r", "", $str );
  $str = addcslashes($str,'/');
  return $str;
}

function resize($in_file, $out_file, $new_width, $new_height=FALSE)
{
  $image = null;
  $extension = strtolower(preg_replace('/^.*\./', '', $in_file));
  switch($extension)
  {
    case 'jpg':
    case 'jpeg':
      $image = imagecreatefromjpeg($in_file);
    break;
    case 'png':
      $image = imagecreatefrompng($in_file);
    break;
    case 'gif':
      $image = imagecreatefromgif($in_file);
    break;
  }
  if(!$image || !is_resource($image)) return false;


  $width = imagesx($image);
  $height = imagesy($image);
  if($new_height === FALSE)
  {
    $new_height = (int)(($height * $new_width) / $width);
  }

  
  $new_image = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

  $ret = imagejpeg($new_image, $out_file, 80);

  imagedestroy($new_image);
  imagedestroy($image);

  return $ret;
}


function cut( $Str, $Length,$sss = '' ) {
  //$Str为截取字符串，$Length为需要截取的长度
  global $s;
  $i = 0;
  $l = 0;
  $ll = strlen( $Str );
  $s = $Str;
  $f = true;
  //if(isset($Str{$i}))
  while ( $i <= $ll ) {
    if ( ord( $Str{$i} ) < 0x80 ) {
      $l++; $i++;
    } else if ( ord( $Str{$i} ) < 0xe0 ) {
      $l++; $i += 2;
    } else if ( ord( $Str{$i} ) < 0xf0 ) {
      $l += 2; $i += 3;
    } else if ( ord( $Str{$i} ) < 0xf8 ) {
      $l += 1; $i += 4;
    } else if ( ord( $Str{$i} ) < 0xfc ) {
      $l += 1; $i += 5;
    } else if ( ord( $Str{$i} ) < 0xfe ) {
      $l += 1; $i += 6;
    }

    if ( ( $l >= $Length - 1 ) && $f ) {
      $s = substr( $Str, 0, $i );
      $f = false;
    }

    if ( ( $l > $Length ) && ( $i < $ll ) ) {
      // $s = $s . '...'; break; //如果进行了截取，字符串末尾加省略符号“...”
      $s = $s . $sss; break; //如果进行了截取，字符串末尾加省略符号“...”
    }
  }
  return $s;
}

function callhttp( $url, $params, $method = 'GET', $header = array(), $multi = false ) {
  $opts = array(
    CURLOPT_TIMEOUT        => 5,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    // CURLOPT_REFERER => _REFERER_,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_USERAGENT     => _USERAGENT_,
    CURLOPT_HTTPHEADER     => $header
  );
  $url_arr = parse_url($url);
  if (!$url_arr['query']) {
    $url = $url.'?';
  }
  if ( $params ) {
    $param = http_build_query( $params );
  }
  /* 根据请求类型设置特定参数 */
  switch ( strtoupper( $method ) ) {
  case 'GET':
    $opts[CURLOPT_URL] = $url.'&'.$param;
    break;
  case 'POST':
    //判断是否传输文件
    // $params = $multi ? $params : http_build_query( $params );
    $opts[CURLOPT_URL] = $url;
    $opts[CURLOPT_POST] = 1;
    $opts[CURLOPT_POSTFIELDS] = $params;
    break;
  default:
    $opts[CURLOPT_URL] = $url.$param;
    break;
  }
  /* 初始化并执行curl请求 */
  $ch = curl_init();
  curl_setopt_array( $ch, $opts );
  $data  = curl_exec( $ch );
  $error = curl_error( $ch );
  curl_close( $ch );
  if ( $error ) throw new Exception( '请求发生错误：' . $error );
  return  $data;
}

function getsegment( $title ) {
  include_once COMMON_PATH.'/segment/pscws4.class.php';
  // load("@.segment.pscws4.class");
  $pscws = new PSCWS4();
  $pscws->set_dict( COMMON_PATH.'/segment/scws/dict.utf8.xdb' );
  $pscws->set_rule( COMMON_PATH.'/segment/scws/rules.utf8.ini' );
  $pscws->set_ignore( true );
  $pscws->send_text( $title );
  $words = $pscws->get_tops( 20 );
  $tags = array();
  foreach ( $words as $val ) {
    if ( strlen( $val['word'] ) > 1 )
      $tags[] = $val['word'];
  }
  $pscws->close();
  return $tags;
}
//替换微信内容
function replacewx( $input, $wxdata ) {
  if ( !$input ) return;
  $input = str_replace( "Location_X", $wxdata['Location_X'], $input );
  $input = str_replace( "Location_Y", $wxdata['Location_Y'], $input );
  $input = str_replace( "FromUserName", $wxdata['FromUserName'], $input );
  $input = str_replace( "keywords", $wxdata['keywords'], $input );
  $input = str_replace( "keywords", $wxdata['Content'], $input );
  $input = str_replace( "Url", $wxdata['Url'], $input );

  $input = str_replace( "_lat_", $wxdata['Location_X'], $input );
  $input = str_replace( "_lng_", $wxdata['Location_Y'], $input );
  $input = str_replace( "_wxid_", $wxdata['FromUserName'], $input );
  return $input;
}
function delspace( $str ) {
  $str = str_replace( "&nbsp;", " ", $str );
  return preg_replace( "/[\s]{2,}/", "", $str );
}
?>